#include "No7pch.h"
#include "Buffer.h"

#include "No7/Core/Renderer/Renderer.h"
#include "No7/Core/Platform/OpenGL/OpenGLBuffer.h"
#include "No7/Core/CoreMacros.h"

nmspc::VertexBuffer* nmspc::VertexBuffer::create(float* vertices, std::uint32_t size)
{
	switch (nmspc::Renderer::getAPI())
	{
	case nmspc::RendererAPI::API::None:
		NO7_CORE_ASSERT(false, "RendererAPI::None is currently not supported!");
		return nullptr;
	case nmspc::RendererAPI::API::OpenGL:
		return new nmspc::OpenGLVertexBuffer(vertices, size);
	}
	NO7_CORE_ASSERT(false, "Unknown RendererAPI!");
	return nullptr;
}

nmspc::IndexBuffer* nmspc::IndexBuffer::create(std::uint32_t* indices, std::uint32_t size)
{
	switch (nmspc::Renderer::getAPI())
	{
	case nmspc::RendererAPI::API::None:
		NO7_CORE_ASSERT(false, "RendererAPI::None is currently not supported!");
		return nullptr;
	case nmspc::RendererAPI::API::OpenGL:
		return new nmspc::OpenGLIndexBuffer(indices, size);
	}
	NO7_CORE_ASSERT(false, "Unknown RendererAPI!");
	return nullptr;
}

nmspc::BufferLayout::BufferLayout(const std::initializer_list<nmspc::BufferElement>& elements) :
	elements(elements)
{
	this->calculateOffsetsAndStride();
}

void nmspc::BufferLayout::calculateOffsetsAndStride()
{
	std::uint32_t offset = 0;
	this->stride = 0;
	for (auto& element : this->elements)
	{
		element.offset = offset;
		offset += element.size;
		stride += element.size;
	}
}
